package com.lh.lock;

import org.openjdk.jol.info.ClassLayout;

import java.util.concurrent.TimeUnit;

/**
 * -XX :+useBiasedLocking 开启偏向锁(默认)
 * -XX :-useBiasedLocking 关闭偏向锁
 * -XX :BiasedLockingStartupDelay=0 关闭延迟(偏向锁需几秒钟才能开启)
 */
public class MyObjectTest {

    public static void main(String[] args) throws InterruptedException {

        //noLock(); //无锁状态

       // pianXiangLock(); //偏向锁

        //zhongLock(); //重量级锁

    }

    private static void zhongLock() {
        Object o = new Object();

        new Thread(() -> {
            synchronized (o) {
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }, "A").start();

        new Thread(() -> {
            synchronized (o) {
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }, "B").start();
    }

    private static void pianXiangLock() {
        Object o = new Object();

        new Thread(() -> {
            synchronized (o) {
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }, "A").start();
    }

    private static void noLock() {
        Object o = new Object();

        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }

}
